package com.zuoqi.easywork.moneyManage.service;

import com.zuoqi.common.utils.IMap;
import com.zuoqi.easywork.moneyManage.mapper.TcFundMapper;
import com.zuoqi.easywork.moneyManage.mapper.TcFundUptItemMapper;
import com.zuoqi.easywork.moneyManage.model.TcFund;
import com.zuoqi.easywork.moneyManage.model.TcFundUptItem;
import com.zuoqi.easywork.system.service.OpLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FundService {
    @Autowired
    private TcFundMapper tcFundMapper;
    @Autowired
    private TcFundUptItemMapper tcFundUptItemMapper;
    @Autowired
    private OpLogService opLogService;

    public IMap getFundInfo(){
        IMap result =tcFundMapper.getFundInfo();
        IMap result1 = tcFundUptItemMapper.getFundInSum();
        result.put("fundInSum",result1.getInt("moneySum"));
        return result;
    }
    public int addFund(IMap param){
        IMap result =tcFundMapper.getFundInfo();
        int addMoney = Double.valueOf(param.getString("addMoney")).intValue();
        int newMoney = result.getInt("money") + addMoney;
        //修改账户金额
        TcFund tcFund =new TcFund();
        tcFund.setFundId(result.getString("fund_id"));
        tcFund.setMoney(newMoney);
        //入明细表
        TcFundUptItem tcFundUptItem = new TcFundUptItem();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        tcFundUptItem.setItemId("u"+datetime);
        tcFundUptItem.setItemName("账户融资");
        tcFundUptItem.setMoney(addMoney);
        tcFundUptItem.setOperation("1");
        tcFundUptItem.setOpTime(new Date());
        try {
            tcFundMapper.updateByPrimaryKeySelective(tcFund);
            tcFundUptItemMapper.insert(tcFundUptItem);
            opLogService.addLog(param.getString("updateUser"),"增加公司账户金额","");
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    public List<IMap>getItemList(IMap param){
        return tcFundUptItemMapper.getItemList(param);
    }
}
